using UnityEngine;
namespace DefaultNamespace.Skill
{
    /// <summary>
    /// 弹药
    /// </summary>
    [CreateAssetMenu(menuName = "Skills/Projectile")]
    public class ProjectileSkill:Skill
    {
        public GameObject projectilePrefab;
        public float projectileSpeed;
        public float range;
        
        public override void Activate(GameObject caster)
        {
            if (!IsReady()) return;
            
            // Play VFX/SFX
            if (castingVFX) Instantiate(castingVFX, caster.transform.position, Quaternion.identity);
            if (castSound) AudioSource.PlayClipAtPoint(castSound, caster.transform.position);
            
            // Instantiate projectile
            GameObject projectile = Instantiate(
                projectilePrefab,
                caster.transform.position + caster.transform.forward,
                caster.transform.rotation
            );
            
            // Set up projectile
            ProjectileController pc = projectile.GetComponent<ProjectileController>();
            if (pc)
            {
                pc.Initialize(caster, projectileSpeed, range);
                pc.onHit += (hit) => {
                    if (impactVFX) Instantiate(impactVFX, new Vector3(0,0,0), Quaternion.identity);
                };
            }
            
            StartCooldown();
        }
    }
}